package com.version1_1.dao.ThirdPart.RongCloud.implement;

import com.util.property.Properties;
import com.util.rongcloud.ApiHttpClient;
import com.util.rongcloud.models.FormatType;
import com.version1_1.dao.ThirdPart.RongCloud.RongCloudDao;
import com.version1_1.dao.UserDao;
import com.version1_1.model.User;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * Created by reeves on 2015/12/2.
 */

@Repository
public class RongCloudDaoImpl implements RongCloudDao {

    private static final Logger LOGGER = Logger.getLogger(RongCloudDaoImpl.class);

    @Autowired
    private UserDao userDao;

    @Override
    public boolean refreshUser(int userId, String userName, String portraitUri) {

        User user = userDao.getUserInfo(userId);
        String refreshUserCode = "";
        try {
            refreshUserCode = ApiHttpClient.refreshUser(Properties.RongCloud_appKey, Properties.RongCloud_appSecret, String.valueOf(userId), user.getUserName(), user.getUserPhoto(), FormatType.json).toString();
        } catch (Exception e) {
            LOGGER.error("调用融云API接口，refreshUser出错...", e);
        }

        // 测试返回码是否正确
        if (JSONObject.fromObject(refreshUserCode).getInt("code") == 200) {
            return true;
        } else {
            //LOGGER.error("调用融云API接口刷新用户信息出错，融云返回结果码不是200,返回码: " + JSONObject.fromObject(refreshUserCode).getInt("code"));
            return false;
        }

    }
}
